package com.hao.controller;

import com.hao.dispatcher.*;
import com.hao.model.Computer;
import com.hao.model.PCBProcess;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class MainController {

    /**
     * 展示主页
     */
    @RequestMapping("homePage")
    public String homePage(HttpSession session,
                           Model model) {
        Computer computer = (Computer)session.getAttribute("computer");
        if (computer == null) {
            computer = new Computer(new RRDispatcher());//默认使用RR算法
            session.setAttribute("computer", computer);
        }
        if (computer.getQueueManager().getReadyQueue().size() <= 0) {
            model.addAttribute("tips", "当前就绪队列任何进程！");
        }
        model.addAttribute("latestRunningPCBProcess", computer.getLatestRunningPCBProcess());
        model.addAttribute("systemTime", computer.getSystemTime());
        model.addAttribute("readyQueue", computer.getQueueManager().getReadyQueue());

//        				<option value="1" selected>轮转调度</option>
//				<option value="2">优先数调度</option>
//				<option value="3">最短进程优先</option>（非抢占）
//				<option value="4">最短剩余时间</option>
        Dispatcher dispatcher = computer.getDispatcher();
        Integer choice = 0;
        if (dispatcher instanceof RRDispatcher) {
            choice = 1;
        } else if (dispatcher instanceof PriorityFirstDispatcher) {
            choice = 2;
        } else if (dispatcher instanceof SPNDispatcher) {
            choice = 3;
        } else {
            choice = 4;
        }
        model.addAttribute("choice", choice);

        return "homePage";
    }

    /**
     *创建进程
     */
    @RequestMapping("createProcess")
    public String createProcess(HttpSession session,
                                Model model) {
        Computer computer = (Computer)session.getAttribute("computer");
        PCBProcess pcbProcess = computer.createPCBProcess();
        if (pcbProcess == null) {
            model.addAttribute("tips", "已经达到最大进程数！不能再创建");
        } else {
            model.addAttribute("tips", "创建进程成功！");
        }
        //----------------------------------------------------------------
        if (computer.getQueueManager().getReadyQueue().size() <= 0) {
            model.addAttribute("tips", "当前就绪队列任何进程！");
        }
        model.addAttribute("latestRunningPCBProcess", computer.getLatestRunningPCBProcess());
        model.addAttribute("systemTime", computer.getSystemTime());
        model.addAttribute("readyQueue", computer.getQueueManager().getReadyQueue());

//        				<option value="1" selected>轮转调度</option>
//				<option value="2">优先数调度</option>
//				<option value="3">最短进程优先</option>（非抢占）
//				<option value="4">最短剩余时间</option>
        Dispatcher dispatcher = computer.getDispatcher();
        Integer choice = 0;
        if (dispatcher instanceof RRDispatcher) {
            choice = 1;
        } else if (dispatcher instanceof PriorityFirstDispatcher) {
            choice = 2;
        } else if (dispatcher instanceof SPNDispatcher) {
            choice = 3;
        } else {
            choice = 4;
        }
        model.addAttribute("choice", choice);
        //---------------------------------------------------------------
        return "homePage";//@todo return "forward:/homePage" 无效
    }

    /**
     * 执行时间片
     */
    @RequestMapping("execute")
    public String execute(HttpSession session,
                          Model model) {
        Computer computer = (Computer)session.getAttribute("computer");
        computer.execute();
        //----------------------------------------------------------------
        if (computer.getQueueManager().getReadyQueue().size() <= 0) {
            model.addAttribute("tips", "当前就绪队列任何进程！");
        }
        model.addAttribute("latestRunningPCBProcess", computer.getLatestRunningPCBProcess());
        model.addAttribute("systemTime", computer.getSystemTime());
        model.addAttribute("readyQueue", computer.getQueueManager().getReadyQueue());

//        				<option value="1" selected>轮转调度</option>
//				<option value="2">优先数调度</option>
//				<option value="3">最短进程优先</option>（非抢占）
//				<option value="4">最短剩余时间</option>
        Dispatcher dispatcher = computer.getDispatcher();
        Integer choice = 0;
        if (dispatcher instanceof RRDispatcher) {
            choice = 1;
        } else if (dispatcher instanceof PriorityFirstDispatcher) {
            choice = 2;
        } else if (dispatcher instanceof SPNDispatcher) {
            choice = 3;
        } else {
            choice = 4;
        }
        model.addAttribute("choice", choice);
        //---------------------------------------------------------------
        return "homePage";
    }

    /**
     * 选择调度策略
     */
    @RequestMapping("choice")
    public String choice(HttpSession session,
                         Model model,
                         @RequestParam("choice") int choice) {
        Computer computer = (Computer)session.getAttribute("computer");
        switch (choice) {
            case 1 : computer.setDispatcher(new RRDispatcher());break;
            case 2 : computer.setDispatcher(new PriorityFirstDispatcher());break;
            case 3 : computer.setDispatcher(new SPNDispatcher());break;
            case 4 : computer.setDispatcher(new SRTDispatcher());break;
            default: break;
        }
        //----------------------------------------------------------------
        if (computer.getQueueManager().getReadyQueue().size() <= 0) {
            model.addAttribute("tips", "当前就绪队列任何进程！");
        }
        model.addAttribute("latestRunningPCBProcess", computer.getLatestRunningPCBProcess());
        model.addAttribute("systemTime", computer.getSystemTime());
        model.addAttribute("readyQueue", computer.getQueueManager().getReadyQueue());

//        				<option value="1" selected>轮转调度</option>
//				<option value="2">优先数调度</option>
//				<option value="3">最短进程优先</option>（非抢占）
//				<option value="4">最短剩余时间</option>
        Dispatcher dispatcher = computer.getDispatcher();

        if (dispatcher instanceof RRDispatcher) {
            choice = 1;
        } else if (dispatcher instanceof PriorityFirstDispatcher) {
            choice = 2;
        } else if (dispatcher instanceof SPNDispatcher) {
            choice = 3;
        } else {
            choice = 4;
        }
        model.addAttribute("choice", choice);
        //---------------------------------------------------------------
        return "homePage";
    }
}
